import datetime
from enum import Enum
from typing import List

from pydantic import BaseModel, validator, Field

headers = {
    'Accept': '*/*',
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
    'Host': 'navi.cnki.net',
    'Origin': 'https://navi.cnki.net',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
}


# api 交互 model
############### 下载moudel ####################
class DownJournalId(BaseModel):
    page: str = ""
    type: str = ""
    subject: str = ""
    marks: List[str] = []


class DownYearModel(BaseModel):
    pykm: str = ""
    marks: List[str] = []


class DownIssueModel(BaseModel):
    pubyear: str = ""
    num: str = ""
    pykm: str = ""
    pageIdx: str = ""
    marks: List[str] = []


class DownOutlineModel(BaseModel):
    pykm: str = ""
    pcode: str = ""
    type: str = "2"
    pageIdx: str = ""
    marks: List[str] = []


class DataArticleModel(BaseModel):
    marks: List[str] = []
    filename: str = ''


class DataRefModel(BaseModel):
    marks: List[str] = []
    filename: str = ''
    year: str = ''
    vl: str = ''
    uid: str = ''


class DataRefPageModel(BaseModel):
    marks: List[str] = []
    filename: str = ''
    year: str = ''
    vl: str = ''
    uid: str = ''
    curdbcode: str = ""
    page: str = ""


class DataCitedPageModel(BaseModel):
    marks: List[str] = []
    filename: str = ''
    dbcode: str = ''  # CJFD
    dbname: str = ''  # CJFDLAST2020 以前只有年
    vl: str = ''
    uid: str = ''  # Referer需要 一般学校代理会有
    curdbcode: str = ""  # 最后这两个参数翻页时需要
    page: str = ""


########### 解析model ##############

class VerificationRef(BaseModel):
    html: str = ""
    refcount: int = 0


class VerificationArticle(BaseModel):
    marks: List[str] = []
    html: str = ""


########### 保存model ##############

class SaveJournalIdModel(BaseModel):
    page: str = ""
    html: str = ""


class SaveJournalSubjectModel(BaseModel):
    page: str = ""
    subject: str = ""
    html: str = ""


class SaveYearModel(BaseModel):
    pykm: str = ""
    html: str = ""


class SaveOutlineModel(BaseModel):
    pykm: str = ""
    pageindex: str = ""
    html: str = ""


class SaveIssueModel(BaseModel):
    pubyear: str = ""
    num: str = ""
    pykm: str = ""
    page: str = ""
    html: str = ""


##*****************************##
class ModelName(str, Enum):
    default = "default"
    update = "update"
    delete = "delete"
    replace = "replace"


class HtmlModel(BaseModel):
    action: ModelName = "default"
    html: str = ""


class JsonModel(BaseModel):
    action: ModelName = "default"
    html: dict = {}


class RedownModel(BaseModel):
    articlehtml: HtmlModel = HtmlModel()
    refhtml: JsonModel = JsonModel()
    refcounthtml: JsonModel = JsonModel()
    overseahtml: HtmlModel = HtmlModel()
    re_down_num: str = "0"


class SaveHtmlModel(BaseModel):
    articlehtml: HtmlModel = HtmlModel()
    refhtml: JsonModel = JsonModel()
    refcounthtml: JsonModel = JsonModel()
    overseahtml: HtmlModel = HtmlModel()
    re_down_num: str = "1"
    filename: str = ""
    redown: RedownModel = RedownModel()


##*****************************##

class VerificationMongodb(BaseModel):
    marks: List[str] = []
    marks_oversea: List[str] = []
    filename: str = ""


class MongoHtmlModel(BaseModel):
    update_time: str = str(datetime.datetime.now())
    html: str = ""

    @validator('update_time', pre=True)
    def split_str(cls, v):
        return str(v)


class MongoJsonModel(BaseModel):
    update_time: str = str(datetime.datetime.now())
    html: dict = {}

    @validator('update_time', pre=True)
    def split_str(cls, v):
        return str(v)


class CnkiArticleMongoRedownModel(BaseModel):
    article: MongoHtmlModel = MongoHtmlModel()
    refdicts: MongoJsonModel = MongoJsonModel()
    refcount: MongoJsonModel = MongoJsonModel()
    oversea: MongoHtmlModel = MongoHtmlModel()
    re_down_num: str = "0"

    @validator('*', pre=True)
    def deal_none(cls, v, field):
        if v is None or v == {} or v == "":
            return field.default
        return v


# mongodb表对应的moudel 用于与mongodb对接
class CnkiArticleMongoModel(BaseModel):
    article: MongoHtmlModel = MongoHtmlModel()
    refdicts: MongoJsonModel = MongoJsonModel()
    refcount: MongoJsonModel = MongoJsonModel()
    oversea: MongoHtmlModel = MongoHtmlModel()
    re_down_num: str = "1"
    id: str = Field("", alias="_id")
    redown: CnkiArticleMongoRedownModel = CnkiArticleMongoRedownModel()
    update_time: str = ""
    create_time: str = ""

    @validator('*', pre=True)
    def deal_none(cls, v, field):
        if v is None or v == {} or v == "":
            return field.default
        return v

    @validator('update_time', 'create_time', pre=True)
    def split_str(cls, v):
        return str(v)

####################################

class SaveOnlineArticleModel(BaseModel):
    id: str = ''
    html: str = ''
    html_oversea: str = ''
    filename: str = ''